package samber

import (
	"fmt"
	"github.com/samber/lo"
	"time"
)

// NewDebounce 开一个实例 执行一次在指定时间之后? 他这个例子没太懂或者说符合预期
func NewDebounce() {
	debounce, cancel := lo.NewDebounce(time.Millisecond, func() {
		println("called once")
	})

	debounce()
	debounce()
	debounce()
	debounce()
	debounce()

	time.Sleep(2 * time.Millisecond)

	cancel()
}

func NewDebounceBy() {
	debounce, cancel := lo.NewDebounceBy(time.Millisecond, func(key string, count int) {
		println(key + " called once")
	})
	debounce("samuel")
	debounce("samuel")
	debounce("john")
	debounce("samuel")
	debounce("samuel")
	time.Sleep(2 * time.Millisecond)
	cancel("samuel")
	cancel("john")
}

// None 子集不在list1中
func None() {
	list1 := []int64{1, 2, 3, 4}
	list2 := []int64{5, 6}
	res := lo.None(list1, list2)
	fmt.Printf("%v \n", res) // true
}

// NoneBy 跟EveryBy相反
func NoneBy() {
	list1 := []int64{1, 2, 3, 4}
	res := lo.NoneBy(list1, func(item int64) bool {
		return item > 5
	})
	fmt.Printf("%v \n", res) // true
}

// Nth 类似Python操作
func Nth() {
	list := []int64{1, 2, 3, 4}
	res, err := lo.Nth(list, 2)
	fmt.Printf("%v %v\n", res, err) // 3 <nil>
	res, err = lo.Nth(list, -1)
	fmt.Printf("%v %v\n", res, err) // 4 <nil>
	res, err = lo.Nth(list, -5)
	fmt.Printf("%v %v\n", res, err) // 0 nth: -5 out of slice bounds
}
